[#ftl]	
[@define "agile._Moveable" "" "move=dojo/dnd/move"]

	constructor: function(){
		this.metadata("moveable",true);
		//aspect.after(this,"startup",lang.hitch(this,"initMoveable"));
	}
	,init:function(){
		//console.log(this.declaredClass,"agile._Moveable[startup]...",this._agileMoveableStartup);
		//internal flag to avoid multiple execution 
		if(this._initMoveable){
			return;
		}
		this._initMoveable = true;
		
		////console.log(this.declaredClass,"moveable:",this.metadata("moveable"),"constrainedMoveable:",this.metadata("constrainedMoveable"));
		if(this.metadata("moveable")){
			var moveable = new move.parentConstrainedMoveable(this.domNode,{within: true});
			this.own(
				on(moveable,"onMoving",lang.hitch(child,function(mover,leftTop){
					//this.onMoving(leftTop.l,leftTop.t);
				}))
				,on(moveable,"onMoveStart",lang.hitch(child,function(){				
					this.coordBeforeMove = agile.calcSize(this.domNode);				
				}))
				,on(moveable,"onMoveStop",lang.hitch(child,function(){				
					var coordAfterMove = agile.calcSize(this.domNode);				
					if(this.coordBeforeMove.l != coordAfterMove.l || this.coordBeforeMove.t != coordAfterMove.t){					
						this.metadata("style.left",coordAfterMove.l);
						this.metadata("style.top",coordAfterMove.t);
						this.onMoveStop(coordAfterMove.l,coordAfterMove.t);				
					}
				}))
			);
		}
	}
	,onMoveStop:function(left,top){
		//console.log(this.declaredClass,"agile._Moveable[onMoveStop]...",left,top);
	}
	
	,onMoving:function(left,top){
				
		var deltaLeft = this.style1("left") - left;
		var deltaTop = this.style1("top") - top;
		
		//moving all siblings selected as well
		var parent = this.getParent();
		if(parent){
			var stop = false;
			var parentCoord = agile.calcSize(parent.domNode);
			
			//check whether any sibling reaches the border of the parent container, stop moving if true
			var selectedSiblings = parent.getChildren("selected");			
			for(var i = 0; i < selectedSiblings.length; i++){								
				var sibling = selectedSiblings[i];
				if(sibling.style1("left") - deltaLeft <= 0 || sibling.style1("left") - deltaLeft + sibling.style1("width")>= parentCoord.w
				|| sibling.style1("top")  - deltaTop <= 0  || sibling.style1("top")  - deltaTop + sibling.style1("height")>= parentCoord.h){
					stop = true;
					break;
				}
			}	
		
			if(!stop){
				array.forEach(this.selectedSiblings,function(widget){
					widget.style1("left",widget.style1("left") - deltaLeft);
					widget.style1("top",widget.style1("top") - deltaTop);	
				});					
			}else{
				//TODO:fix me:should stop this from moving
				this.style1("left",left);
				this.style1("top",top);	
			}
		}
				
		////console.log("@@@@@this.selectedWidgets",this.selectedWidgets);		
//		topic.publish("agile.widget.changed.styles",[this,"style.left",left]);	
//		topic.publish("agile.widget.changed.styles",[this,"style.top",top]);
	}

[/@]
